﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Users;
using Lists;
using Sim;
using System.Xml;
using System.Xml.Serialization;

namespace Lists
{
    public class DDList
    {
        public struct DD //Structen för Divelistan
        {
            public string diveCode { get; set; }
            public int diveHeight { get; set; }
            public string diveDescription { get; set; }
            public double diveDifficulty { get; set; }
        };

        static public List<DD> ddList { get; set; }

        static DDList()
        {
            ddList = new List<DD>();
            //DeserializeFromXML();
        }

        public void Readfromfile() // Läser in alla hoppkoder till en lista
        {
            string line = "";
            using (StreamReader sr = new StreamReader("JumpList.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    DD dd = new DD();
                    string[] split = line.Split(';');
                    dd.diveCode = split[0];
                    dd.diveHeight = Convert.ToInt32(split[1]);
                    dd.diveDescription = split[2];
                    dd.diveDifficulty = Convert.ToDouble(split[3]);
                    ddList.Add(dd);
                }
                sr.Close();
            }
        }

        static public void DeserializeFromXML()
        {
            ddList.Clear();
            XmlSerializer deserializer = new XmlSerializer(typeof(List<DD>), new XmlRootAttribute("DIFFICULTIES"));
            TextReader textReader = new StreamReader("DDList.xml");
            ddList = (List<DD>)deserializer.Deserialize(textReader);
            textReader.Close();
        }

        static public void SerializeToXML()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<DD>), new XmlRootAttribute("DIFFICULTIES"));
            TextWriter textWriter = new StreamWriter("DDList.xml");
            serializer.Serialize(textWriter, ddList);
            textWriter.Close();
        }
    }
}
